package com.travel.server.manager.impl.command.customer;

import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.UpdateCustomerBaseInfoRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.dal.MemberGradeDal;
import com.travel.server.manager.impl.sal.CustomerSal;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:06
 * @Desc:
 */
@Component
@Slf4j
public class UpdateCustomerBaseInfoCommand extends AbstractCommand<UpdateCustomerBaseInfoRequest, EmptyResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Override
    protected void checkInput(UpdateCustomerBaseInfoRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateCustomerBaseInfoRequest request) {
        checkInput(request);

        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        CustomerInfo customerInfo = customerDal.selectById(request.getId());
        customerInfo.setName(request.getName());
        customerInfo.setWechatNick(request.getNick());
        customerInfo.setMobile(request.getMobile());
        customerInfo.setSex(request.getSex());
        if(StringUtils.isNotBlank(request.getBirthDate())){
            customerInfo.setBirthDate(DateUtil.strToDate(request.getBirthDate(), DateUtil.YYYY_MM_DD));
        }
        customerInfo.setWechatNumber(request.getWechatNumber());
        customerInfo.setLiveCountry(request.getLiveCountry());
        customerInfo.setLiveProvince(request.getLiveProvince());
        customerInfo.setLiveCity(request.getLiveCity());
        customerInfo.setLiveArea(request.getLiveArea());
        customerInfo.setLiveAddress(request.getLiveAddress());
        customerInfo.setMail(request.getMail());
        customerInfo.setGarmentSize(request.getGarmentSize());
        customerInfo.setNote(request.getNote());
        if(request.getMemberGradeId() != null){
            MemberGradeInfo memberGradeInfo = memberGradeDal.selectById(request.getMemberGradeId());
            customerInfo.setMemberGradeId(request.getMemberGradeId());
            customerInfo.setIsMember(CustomerSal.isMember(memberGradeInfo));
        }
        customerInfo.setUpdateBy(userInfo.getName());
        customerInfo.setUpdateTime(now);

        customerDal.insertOrUpdate(customerInfo);

        return new EmptyResponse();
    }
}
